package com.zxx.good.controller.good;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxx.good.entity.good.GoodCateEntity;
import com.zxx.good.service.good.GoodCateService;
import com.zxx.good.utils.SpliceUtil;
import com.baomidou.mybatisplus.extension.api.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @Description:  控制层
 * @author: zhuangxx
 * @date 2020-07-11
 */
@RestController
@RequestMapping("api/good_cate")
@ApiModel(value="", description="")
@Api(description = "")
public class GoodCateController  {

    @Autowired
    private  GoodCateService service;

    /**
     * 列表查询
     * @author: zhuangxx
     * @param filters
     * @param sorts
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "返回集合", notes = "返回集合")
    public R<List<GoodCateEntity>> list(
            @ApiParam(name = "filters", value = "过滤器，为空检索所有条件", defaultValue = "")
            @RequestParam(value = "filters", required = false) String filters,
            @ApiParam(name = "sorts", value = "排序", defaultValue = "")
            @RequestParam(value = "sorts", required = false) String sorts) throws Exception {
            QueryWrapper<GoodCateEntity> ew = new QueryWrapper();
            SpliceUtil.search(filters, sorts, ew);
            return R.ok(service.list(ew));
            }

/**
 * 分页查询
 * @author: zhuangxx
 * @param id
 * @return
 */
@GetMapping("/{id}")
@ApiOperation(value = "获取信息", notes = "获取信息")
public  R<GoodCateEntity> getById(
@ApiParam(name = "id", value = "id", defaultValue = "")
@PathVariable Integer id){
        return R.ok(service.getById(id));
        }

    /**
     * 分页查询
     * @author: zhuangxx
     * @param filters
     * @param sorts
     * @param size
     * @param page
     * @return
     */
    @GetMapping("/listPage")
    @ApiOperation(value = "返回集合", notes = "返回集合")
    public R<IPage<GoodCateEntity>> listPage(
            @ApiParam(name = "filters", value = "过滤器，为空检索所有条件", defaultValue = "")
            @RequestParam(value = "filters", required = false) String filters,
            @ApiParam(name = "sorts", value = "排序", defaultValue = "")
            @RequestParam(value = "sorts", required = false) String sorts,
            @ApiParam(name = "size", value = "分页大小", defaultValue = "15")
            @RequestParam(value = "size", required = false) int size,
            @ApiParam(name = "page", value = "页码", defaultValue = "1")
            @RequestParam(value = "page", required = false) int page) throws Exception {
        QueryWrapper<GoodCateEntity> ew = new QueryWrapper();
        SpliceUtil.search(filters, sorts, ew);
        Page<GoodCateEntity > page1 = new Page(page, size);
        IPage result = service.page(page1, ew);
        return R.ok(result);
    }



    /**
     * 新增
     * @author: zhuangxx
     * @param goodCateEntity
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增", notes = "新增")
    public R<Boolean> create(
            @ApiParam(name = "json_model", value = "", defaultValue = "")
            @RequestBody @Validated GoodCateEntity goodCateEntity) {
        return R.ok(service.save(goodCateEntity));
    }

    /**
     * 编辑
     * @author: zhuangxx
     * @param goodCateEntity
     * @return
     */
    @PutMapping
    @ApiOperation(value = "编辑", notes = "编辑")
    public R<Boolean> edit(
            @ApiParam(name = "json_model", value = "", defaultValue = "")
            @RequestBody @Validated GoodCateEntity goodCateEntity){
        return R.ok(service.updateById(goodCateEntity));
    }

    /**
     * 删除
     * @author: zhuangxx
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除", notes = "删除")
    public  R<Boolean> delete(
            @ApiParam(name = "id", value = "id", defaultValue = "")
            @PathVariable Integer id){
        return R.ok(service.removeById(id));
    }

    /**
     * 批量删除
     * @author: zhuangxx
     * @param idList
     * @return
     */
    @DeleteMapping("/delBatch/{idList}")
    @ApiOperation(value = "批量删除", notes = "批量删除")
    public R delBatch(@ApiParam(name = "idList", value = "idList", defaultValue = "")
            @PathVariable List<Integer> idList) {
        return R.ok(service.removeByIds(idList));
    }
}
